﻿using System.Collections.Generic;
using System.Linq;

namespace CommonUtils.CodeFactory
{
    public static class IdeUtil
    {
        public static void ShowSlnPath()
        {
            ConsoleUtil.Write(
                "  <Target Name=\"PreBuild\" BeforeTargets=\"PreBuildEvent\">		  " +
                "    <Exec Command=\"echo $(SolutionPath)&gt;$(ProjectDir)sln.txt\" />" +
                "  </Target>														  "
                              );
        }

        /// <summary>
        /// 获取sln包含的项目
        /// </summary>
        public static Dictionary<string, string> GetSlnFolders(string path)
        {
            var sln = FileUtil.GetInfo(path);
            var content = sln.GetText();
            var lines = content.Substrings("\", \"", ".csproj\", \"", false);
            var folders = new Dictionary<string, string>();
            foreach (var line in lines)
                folders.Add(FileUtil.GetName(line), FileUtil.GetFolder(FileUtil.GetFolder(path).Combine(line)));
            return folders;
        }

        /// <summary>
        /// 删除注释 用于反编译出来的代码的无用注释
        /// </summary>
        public static void RemoveJavaComment(string folderPath, string extension = ".java")
        {
            var folder = FolderUtil.GetInfo(folderPath);
            var files = folder.GetAllFiles();
            foreach (var file in files)
            {
                if (file.Extension != extension)
                    continue;
                var text = file.GetText();
                file.Save(text.RemoveBetween("/*", "*/"));
            }
        }

        /// <summary>
        /// 框架升级 2.0-2.2
        /// </summary>
        public static void UpgradeCoreVersion(string folder)
        {
            var files = FolderUtil.GetInfo(folder).GetAllFiles();
            files = files.Where(m => m.Extension == ".pubxml" || m.Extension == ".csproj").ToArray();
            foreach (var file in files)
                file.Save(file.GetText()
                    .Replace("netcoreapp2.0", "netcoreapp2.2")
                    .Replace("Microsoft.AspNetCore.All", "Microsoft.AspNetCore.App_deleteversion")
                    .Replace("Microsoft.VisualStudio.Web.CodeGeneration.Design", "_deleteline")
                    .Replace("Microsoft.VisualStudio.Web.CodeGeneration.Tools", "_deleteline"));
        }
    }
}
